<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 员工信息
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
          &nbsp;&nbsp;<el-button type="primary" size="small" @click="addVisible = true">添加员工</el-button>&nbsp;
          <el-button type="primary" size="small" @click="dels">批量删除</el-button>&nbsp;
      <div class="handle-box">
        <!-- <el-button
          type="primary"
          icon="el-icon-delete"
          class="handle-del mr10"
          @click="delAllSelection"
          >批量删除</el-button
        > -->
        <el-input v-model="name" placeholder="员工名称" class="handle-input mr10"></el-input>
        <el-input v-model="department" placeholder="员工部门" class="handle-input mr10"></el-input>
        <el-select v-model="status" placeholder="员工状态">
            <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
        </el-select>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        height="500px"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="40" align="center"></el-table-column>
        <el-table-column property="id" label="ID" width="50" align="center"></el-table-column>
        <el-table-column property="name" label="姓名" width="55" align="center"></el-table-column>
        <el-table-column property="department" label="部门名称" width="82" align="center"></el-table-column>
        <el-table-column property="telephone" label="电话" width="110" align="center"></el-table-column>
        <el-table-column property="emailAddress" label="邮箱" width="110" align="center"></el-table-column>
        <el-table-column property="position" label="职位名称" width="80" align="center"></el-table-column>
        <el-table-column property="description" label="员工描述" width="150" align="center"></el-table-column>
        <el-table-column property="status" width="55" label="状态" align="status" :formatter="fgf"></el-table-column>
        <el-table-column property="createdDate" label="创建时间"></el-table-column>
        <el-table-column property="modifiedDate" label="更新时间"></el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template #default="scope">
            <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button type="text" @click="open(scope.$index, scope.row)">删除</el-button>
            <!-- <el-button
              type="text"
              icon="el-icon-delete"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            > -->
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageIndex"
          :page-sizes="[1, 2, 3, 5]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageTotal">
        </el-pagination>
      </div>
    </div>
    <!-- 添加弹出框 -->
        <el-dialog v-bind="$attrs" title="添加" v-model="addVisible">
          <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="150px">
            <el-row :gutter="15">
              <el-col :span="15">
                <el-form-item label="员工名称" prop="Name">
                  <el-input v-model="formData.Name" placeholder="请输入员工名称" clearable :style="{width: '100%'}"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="15">
                <el-form-item label="部门名称" prop="Department">
                  <el-input v-model="formData.Department" placeholder="请输入部门名称" clearable :style="{width: '100%'}">
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="15">
                <el-form-item label="电话" prop="Telephone">
                  <el-input v-model="formData.Telephone" placeholder="请输入电话" clearable :style="{width: '100%'}">
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="15">
                <el-form-item label="邮箱" prop="EmailAddress">
                  <el-input v-model="formData.EmailAddress" placeholder="请输入邮箱" clearable :style="{width: '100%'}">
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="15">
                <el-form-item label="职位名称" prop="Position">
                  <el-input v-model="formData.Position" placeholder="请输入职位名称" clearable :style="{width: '100%'}">
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="15">
                <el-form-item label="员工描述" prop="Description">
                  <el-input v-model="formData.Description" placeholder="请输入员工描述" clearable :style="{width: '100%'}">
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="15">
                <el-form-item label="员工状态" prop="Status">
                  <el-radio-group v-model="formData.Status" size="medium">
                    <el-radio v-for="(item, index) in statusOptions" :key="index" :label="item.value"
                      :disabled="item.disabled">{{item.label}}</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item size="large">
              <el-button type="primary" @click="submitForm">提交</el-button>
              <el-button @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </el-dialog>

    <!-- 编辑弹出框 -->
    <el-dialog v-bind="$attrs" title="编辑" v-model="editVisible">
      <el-form ref="form" :model="form" :rules="rules" size="medium" label-width="80px">
        <el-row :gutter="15">
          <el-col :span="10">
            <el-form-item label="员工姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入员工姓名" clearable :style="{width: '100%'}">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="部门名称" prop="department">
              <el-input v-model="form.department" placeholder="请输入部门名称" clearable :style="{width: '100%'}">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="员工电话" prop="telephone">
              <el-input v-model="form.telephone" placeholder="请输入员工电话" clearable :style="{width: '100%'}">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="员工邮箱" prop="emailAddress">
              <el-input v-model="form.emailAddress" placeholder="请输入员工邮箱" clearable :style="{width: '100%'}">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="职位名称" prop="position">
              <el-input v-model="form.position" placeholder="请输入职位名称" clearable :style="{width: '100%'}">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="员工描述" prop="description">
              <el-input v-model="form.description" placeholder="请输入员工描述" clearable :style="{width: '100%'}">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="员工状态" prop="status">
                  <el-radio-group v-model="formData.Status" size="medium">
                    <el-radio v-for="(item, index) in statusOptions" :key="index" :label="item.value"
                      :disabled="item.disabled">{{item.label}}</el-radio>
                  </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editVisible = false">取 消</el-button>
          <el-button type="primary" @click="saveEdit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
// ,updateEmp,deleEmp,fanEmp 
import { showEmp,AddEmp,updateEmp,getEmp,deleEmp,deleEmps} from "../../api/employee.js";
export default {
  name: "basetable",
  data() {
    return {
        name: "",
        department: "",
        status: 1,
        pageIndex: 1,
        pageSize: 3,
      options: [
        {
          value: "1",
          label: "在职",
        },
        {
          value: "0",
          label: "离职",
        },
      ],
      tableData: [],
      multipleSelection: [],
      formData: {
        Name: "",
        Department: "",
        Telephone: "",
        EmailAddress: "",
        Position: "",
        Description: "",
        Status: 1,
      },
      delList: [],
            delLists: [],
            addVisible: false,
      editVisible: false,
      pageTotal: 0,
      form: {
        name: "",
        department: "",
        telephone: "",
        emailAddress: "",
        position: "",
        description: "",
        status: 1,
      },
      data() {
        return {
          options: [],
          value: "",
        };
      },
      idx: -1,
      id: -1,
      statusOptions: [{
        "label": "在职",
        "value": 1
      }, {
        "label": "离职",
        "value": 0
      }],
    };
  },
  created() {
    this.Show();
  },
  methods: {
    fgf(row) {
      if (row.status == "1") {
        return <span style="color:green">在职</span>;
      } else {
        return <span style="color:red">离职</span>;
      }
    },
    Show() {
      showEmp({
          PageSize: this.pageSize,
          PageIndex: this.pageIndex,
          Name: this.name,
          Department: this.department,
          Status: this.status,
        })
        .then((res) => {
        console.log(res.data);
        this.tableData = res.data.pagedData;
        this.pageTotal = res.data.totalCount;
      });
    },
   
    // 触发搜索按钮
    handleSearch() {
      this.Show();
    },
    //状态显示
    statusShow(value) {
      if (value == 1) {
        this.status = 1;
        //this.Show();
      } else {
        this.status = 0;
        //this.Show();
      }
    },
    submitForm() {
      AddEmp(this.formData)
      .then((res) => {
        if(res.code>0)
        {
          alert("添加成功");
          this.Show();
        }
        else{
          alert("添加失败");
        }
      });
      this.editVisible = false;
    },
    // // 删除操作
    // handleDelete(index) {
    //   // 二次确认删除
    //   this.$confirm("确定要删除吗？", "提示", {
    //     type: "warning",
    //   })
    //     .then(() => {
    //       this.$message.success("删除成功");
    //       this.tableData.splice(index, 1);
    //     })
    //     .catch(() => {});
    // },

    //删除操作
    open(index,row) {
      alert(row.id);
      deleEmp({
        objId:row.id
      }).then((res) => {
        console.log(res);
        if (res.code == 1) {
          alert("状态修改成功");
          this.Show();
        } else {
          alert("状态修改失败");
          this.Show();
        }
      });
    },
      dels(){
            for(var i=0;i<this.multipleSelection.length;i++){
                this.delLists.push(this.multipleSelection[i].id);
            }
            alert(this.delLists);
            if(confirm("请确认是否进行删除!!!")){
                deleEmps({
                objId: 0+this.delLists,
                }).then((res) => {
                console.log(res);
                if (res.code == 1) {
                    alert("批量删除成功");
                    this.Show();
                } else {
                    alert("批量删除失败");
                    this.Show();
                }
            });
        }
        this.delLists=[];
      },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    delAllSelection() {
      const length = this.multipleSelection.length;
      let str = "";
      this.delList = this.delList.concat(this.multipleSelection);
      for (let i = 0; i < length; i++) {
        str += this.multipleSelection[i].name + " ";
      }
      this.$message.error(`删除了${str}`);
      this.multipleSelection = [];
    },
    // 编辑操作
    handleEdit(index, row) {
      getEmp({
        id:row.id
      })
      .then((res) => {
        console.log(res);
        this.form = res.data;
      });
      this.editVisible = true;
    },
    // 保存编辑
    saveEdit() {
      updateEmp(this.form)
      .then((res) => {
        if (res.code > 0) {
          alert("修改成功");
          this.Show();
        } else {
          alert("修改失败");
        }
      });
      this.editVisible = false;
    },
      // 分页大小
      handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
        this.pageSize = val;
        this.Show();
      },
      //分页页码
      handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
        this.pageIndex = val;
        this.Show();
      },
    resetForm() {
      this.$refs['elForm'].resetFields()
    },
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
